<?php

if (isset($_POST['id'])){//Boton VER
        $dir = "tmp";
        $handle=opendir($dir);

        while (($file = readdir($handle))!==false) {
        //echo "$file <br>";
            //unlink($dir.'/'.$file);
        }
        closedir($handle);

        $file_handle = fopen("conf.txt", "r");
        $line = split(":",fgets($file_handle));

        $hostname= $line[0];
        $db_user= $line[1];
        $db_pass= $line[2];
        $db_name= chop($line[3]);

        fclose($file_handle);
        
       $con = mysql_connect($hostname,$db_user,$db_pass);
       
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        date_default_timezone_set('UTC');
        mysql_select_db($db_name, $con);
        
        $sql = 'SELECT id,nombre,autor,comentario FROM informe WHERE id= "'.$_POST['id'].'";';
        
        $result = mysql_query($sql);
        
        if (mysql_num_rows($result) == 1){
            
            $row = mysql_fetch_array($result);
            
            $id = $row['id'];
            $nombre = $row['nombre'];
            $autor = $row['autor'];
            $txt = $row['comentario'];
            
            require_once('tcpdf/config/lang/spa.php');
            require_once('tcpdf/tcpdf.php');

            // create new PDF document
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF Example 009');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // set default header data
            $pdf->SetHeaderData('gos_logo.jpg', PDF_HEADER_LOGO_WIDTH, 'INFORME'.'', '');

            // set header and footer fonts
            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            //set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            //set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            //set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            //set some language-dependent strings
            $pdf->setLanguageArray($l);

            // -------------------------------------------------------------------

            // add a page
            $pdf->AddPage();
            
            $pdf->Image('graficos/grafico'.$id.'.png', $x=15, $y=30, $w='', $h='', $type='PNG', $link='', $align='', $resize=true, $dpi=200, $palign='', $ismask=false, $imgmask=false, $border=5, $fitbox=true, $hidden=false, $fitonpage=true);
            
            $pdf->SetFont('helvetica', '', 8);

            $pdf->SetXY(20, 100);
            $pdf->Write(0, 'Comentario:', '', 0, 'L', true, 0, false, false, 0);
            $pdf->SetXY(20, 105);
            $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);

            $pdf->Output('tmp/'.$nombre.'_'.$autor.'.pdf', 'F');

            echo 'http://localhost/SSOT_SAT/trunk/tmp/'.$nombre.'_'.$autor.'.pdf';
                    }
}
else{// Vista Previa
    
            $encodeData = $_POST['imagen'];
            //$filename = $_POST['filename'];
            $txt = $_POST['text'];

            $encodeData = substr($encodeData, strpos($encodeData, ',') + 1); //strip the URL of its headers
            $encodeData = str_replace(' ','+',$encodeData);
            $decodeData = base64_decode($encodeData);

            /*
            $handle = fopen('vista_previa.png', 'w+');
            fwrite($handle, $decodeData);
            fclose($handle);
            */

            require_once('tcpdf/config/lang/spa.php');
            require_once('tcpdf/tcpdf.php');

            // create new PDF document
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF Example 009');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // set default header data
            $pdf->SetHeaderData('gos_logo.jpg', PDF_HEADER_LOGO_WIDTH, 'INFORME'.'', '');

            // set header and footer fonts
            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            //set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            //set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            //set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            //set some language-dependent strings
            $pdf->setLanguageArray($l);

            // -------------------------------------------------------------------

            // add a page
            $pdf->AddPage();

            // set JPEG quality
            //$pdf->setJPEGQuality(75);

            // Image method signature:
            // Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false)

            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            // Example of Image from data stream ('PHP rules')
            //$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');

            // The '@' character is used to indicate that follows an image data stream and not an image file name
            //$pdf->Image('@'.$decodeData);

            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            // Image example with resizing
            //$pdf->Image('@'.$decodeData, 15, 30, 185, 65, 'PNG', false, '', true, 150, '', false, false, 1, false, false, false);
            $pdf->Image('@'.$decodeData, $x=15, $y=30, $w='', $h='', $type='PNG', $link='', $align='', $resize=true, $dpi=200, $palign='', $ismask=false, $imgmask=false, $border=5, $fitbox=true, $hidden=false, $fitonpage=true);
            //$pdf->Image('container0.png', $x=15, $y=30, $w='', $h='', $type='PNG', $link='', $align='', $resize=true, $dpi=200, $palign='', $ismask=false, $imgmask=false, $border=5, $fitbox=true, $hidden=false, $fitonpage=true);
            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            // test fitbox with all alignment combinations
            /*
            $horizontal_alignments = array('L', 'C', 'R');
            $vertical_alignments = array('T', 'M', 'B');

            $x = 15;
            $y = 35;
            $w = 30;
            $h = 30;
            // test all combinations of alignments
            for ($i = 0; $i < 3; ++$i) {
                $fitbox = $horizontal_alignments[$i].' ';
                $x = 15;
                for ($j = 0; $j < 3; ++$j) {
                    $fitbox{1} = $vertical_alignments[$j];
                    $pdf->Rect($x, $y, $w, $h, 'F', array(), array(128,255,128));
                    $pdf->Image('../images/image_demo.jpg', $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, $fitbox, false, false);
                    $x += 32; // new column
                }
                $y += 32; // new row
            }

            $x = 115;
            $y = 35;
            $w = 25;
            $h = 50;
            for ($i = 0; $i < 3; ++$i) {
                $fitbox = $horizontal_alignments[$i].' ';
                $x = 115;
                for ($j = 0; $j < 3; ++$j) {
                    $fitbox{1} = $vertical_alignments[$j];
                    $pdf->Rect($x, $y, $w, $h, 'F', array(), array(128,255,255));
                    $pdf->Image('../images/image_demo.jpg', $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, $fitbox, false, false);
                    $x += 27; // new column
                }
                $y += 52; // new row
            }

            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            // Stretching, position and alignment example


            $pdf->Image('../images/image_demo.jpg', '', '', 40, 40, '', '', 'T', false, 300, '', false, false, 1, false, false, false);
            $pdf->Image('../images/image_demo.jpg', '', '', 40, 40, '', '', '', false, 300, '', false, false, 1, false, false, false);

            // -------------------------------------------------------------------
            */
            //Close and output PDF document

            $pdf->SetFont('helvetica', '', 8);

            $pdf->SetXY(20, 100);
            $pdf->Write(0, 'Comentario:', '', 0, 'L', true, 0, false, false, 0);
            $pdf->SetXY(20, 105);
            $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);

            $pdf->Output('vista_previa.pdf', 'F');

            echo 'http://localhost/SSOT_SAT/trunk/vista_previa.pdf';


            /*
            $filename = $_POST['filename'];
            $svg = $_POST['svg'];
            //$encodeData = substr($encodeData, strpos($encodeData, ',') + 1); //strip the URL of its headers
            //$decodeData = base64_decode($encodeData);

            $handle = fopen($filename.".svg", 'x+');
            fwrite($handle, $svg);
            fclose($handle);
            */
            //header("Content-type: image/png");
            /*
            $image = new Imagick();
            $image->readImageBlob(file_get_contents('tux.svg'));
            $image->setImageFormat("png24");
            //$image->resizeImage(1920, 1080, imagick::FILTER_LANCZOS, 1); 
            $image->writeImage('tux.png');
            */

            /*
            require_once('/tcpdf/config/lang/eng.php');
            require_once('/tcpdf/tcpdf.php');

            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            //set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Nicola Asuni');
            $pdf->SetTitle('TCPDF Example 058');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // set default header data
            $pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 058', PDF_HEADER_STRING);

            // set header and footer fonts
            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            //set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            //set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            //set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            //set some language-dependent strings
            $pdf->setLanguageArray($l);

            // ---------------------------------------------------------

            // set font
            $pdf->SetFont('helvetica', '', 10);

            // add a page
            $pdf->AddPage();

            // NOTE: Uncomment the following line to rasterize SVG image using the ImageMagick library.


            $pdf->ImageSVG($file="container1.svg", $x=15, $y=20, $w='150', $h='200', $link='http://www.tcpdf.org', $align='N', $palign='C', $border=1, $fitonpage=true);

            //$pdf->setRasterizeVectorImages(true);
            //$pdf->ImageSVG($file='../images/tux.svg', $x=30, $y=100, $w='', $h=100, $link='', $align='', $palign='', $border=0, $fitonpage=false);


            $pdf->SetFont('helvetica', '', 8);
            $pdf->SetY(195);
            //$txt = '© The copyright holder of the above Tux image is Larry Ewing, allows anyone to use it for any purpose, provided that the copyright holder is properly attributed. Redistribution, derivative work, commercial use, and all other use is permitted.';
            //$pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);

            // ---------------------------------------------------------

            //Close and output PDF document
            $pdf->Output('test1.pdf', 'F');
            */
            }

?>